package com.tang.watermake;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.JoinFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * 窗口联结
 * 1.落在同一时间窗口内才能匹配
 * 2.根据keyBy的key，来进行匹配关联
 * 3.只能拿到匹配上的数据，类似有固定时间范围的inner join
 *
 * @author tang
 * @since 2023/6/30 22:30
 */
public class WindowJoinDemo {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<Tuple2<String, Integer>> dataStream1 = env.fromElements(
                Tuple2.of("a", 1),
                Tuple2.of("a", 2),
                Tuple2.of("b", 1),
                Tuple2.of("c", 1)
        ).assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<Tuple2<String, Integer>>forMonotonousTimestamps()
                        .withTimestampAssigner((element, recordTimestamp) -> element.f1 * 1000L)
        );

        SingleOutputStreamOperator<Tuple3<String, Integer, Integer>> dataStream2 = env.fromElements(
                Tuple3.of("a", 1, 1),
                Tuple3.of("a", 11, 1),
                Tuple3.of("b", 2, 1),
                Tuple3.of("b", 12, 1),
                Tuple3.of("c", 12, 1),
                Tuple3.of("d", 12, 1)
        ).assignTimestampsAndWatermarks(
                WatermarkStrategy
                        .<Tuple3<String, Integer, Integer>>forMonotonousTimestamps()
                        .withTimestampAssigner((element, recordTimestamp) -> element.f1 * 1000L)
        );

        DataStream<String> join = dataStream1.join(dataStream2)
                .where(r1 -> r1.f0)// dataStream1的keyBy
                .equalTo(r2 -> r2.f0)//dataStream2的keyBy
                .window(TumblingEventTimeWindows.of(Time.seconds(10)))
                .apply(new JoinFunction<Tuple2<String, Integer>, Tuple3<String, Integer, Integer>, String>() {

                    /**
                     * 关联上的数据调用join方法
                     *
                     * @param first The element from first input. 前一个流的element
                     * @param second The element from second input. 后一个流的element
                     * @return 合流后的数据
                     * @throws Exception -
                     */
                    @Override
                    public String join(Tuple2<String, Integer> first, Tuple3<String, Integer, Integer> second) throws Exception {
                        return first + "<======>" + second;
                    }
                });
        join.print();

        env.execute();
    }

}
